package com.spring.mvc.mina.test.demo;

import java.nio.channels.SocketChannel;
import java.util.LinkedList;
import java.util.List;

public class EchoWorker implements Runnable {
	
	private List<ServerDataEvent> queue=new LinkedList<ServerDataEvent>();

	public void processData(NioServer server, SocketChannel socket,
			byte[] array, int numRead) {
		byte[] dataCopy=new byte[numRead];
		System.arraycopy(array, 0, dataCopy, 0, numRead);
		synchronized (queue) {
			queue.add(new ServerDataEvent(server,socket,dataCopy));
			queue.notify();
		}
	}

	public void run() {
		ServerDataEvent dataEvent;
		
		while(true){
			synchronized (queue) {
				while(queue.isEmpty()){
					try{
						System.out.println("queue wait()");
						queue.wait();
					} catch(InterruptedException e){
						e.printStackTrace();
					}
				}
				dataEvent=(ServerDataEvent)queue.remove(0);
			}
			dataEvent.server.send(dataEvent.socket,dataEvent.data);
		}
	}

}
